package com.zdp.algorithm.linkedlist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 删除链表第k个结点
 * 在创建链表的同时，跳过要删除的那个结点即可。
 * （如果要先创建完链表，然后再进行删除的话，会超时）
 * @author ZhengDp
 * @Date 2023/2/6 22:49
 */
public class RemoveKThNode {

    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = input.readLine().split(" ");
        String[] s2 = input.readLine().split(" ");
        int length = Integer.parseInt(s1[0]);
        int m  = Integer.parseInt(s1[1]);
        Node head = new Node();
        Node p = head;
        int i = 1;
        for(String val : s2){
            Node cur = new Node(Integer.parseInt(val));
            if(i == m) {
                i++;
                continue;
            }
            cur.next = p.next;
            p.next = cur;
            p = cur;
            i++;
        }
        head = head.next;
        while(head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }

    }
}

/**
 * 删除第k个结点
 * 用这个在牛客会超时
 */
class RemoveKThNodeImpl {

    public Node removeKThNode(Node head,int k ) {
        if(k <= 0 ){
            return head.next;
        }
        if( k == 1) {
            return head.next.next;
        }
        Node p = head;
        for(int i = 0 ; i < k-1  && p != null ; i ++) {
            p = p.next;
        }
        p.next = p.next.next;
        return head.next;

    }
}
